package com.yingpai.fitness.dvp_2_0.main.activity.mine.assmble;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.fragment.mine.assmble.AssmbeAllOrderFragment;
import com.yingpai.fitness.dvp_2_0.main.fragment.mine.assmble.AssmbeFaileOrderFragment;
import com.yingpai.fitness.dvp_2_0.main.fragment.mine.assmble.AssmbeRefundOrderFragment;
import com.yingpai.fitness.dvp_2_0.main.fragment.mine.assmble.AssmbeSuccessOrderFragment;
import com.yingpai.fitness.dvp_2_0.main.fragment.mine.assmble.AssmbingOrderFragment;
import com.yingpai.fitness.entity.TabEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2019/1/17.
 */

public class AssmbleOrderListActivity extends CommonActivity {

    @BindView(R.id.common_tab)
    CommonTabLayout commonTab;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    private String[] mTitles = new String[5];
    private ArrayList<Fragment> mFragments = new ArrayList<>();//存放Fragment
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.dvp_2_0_activity_all_order);
    }

    @Override
    protected void initView() {
        setTitle("拼团订单");
        mTitles[0] = "全部拼团";
        AssmbeAllOrderFragment allOrder = AssmbeAllOrderFragment.newInstance("");
        mFragments.add(allOrder);

        mTitles[1] = "拼团中";
        AssmbingOrderFragment assmbling = AssmbingOrderFragment.newInstance("30,31");
        mFragments.add(assmbling);

        mTitles[2] = "拼团成功";
        AssmbeSuccessOrderFragment success = AssmbeSuccessOrderFragment.newInstance("20");
        mFragments.add(success);

        mTitles[3] = "拼团失败";
        AssmbeFaileOrderFragment faile = AssmbeFaileOrderFragment.newInstance("10");
        mFragments.add(faile);

        mTitles[4] = "退款/售后";
        AssmbeRefundOrderFragment refund = AssmbeRefundOrderFragment.newInstance("40");
        mFragments.add(refund);

        commonTab.setTextSelectColor(getResources().getColor(R.color.common_color));

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        viewpager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        commonTab.setTabData(mTabEntities);

        commonTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewpager.setCurrentItem(position);
                mSetTitle(position);
            }

            @Override
            public void onTabReselect(int position) {
//                if (position == 3)
//                {//生成tab上的小气泡
//                    commonBottomTab.showMsg(3, position++);
//                    UnreadMsgUtils.show(commonBottomTab.getMsgView(3), position++);
//                }
            }
        });

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTab.setCurrentTab(position);
                mSetTitle(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });

        viewpager.setCurrentItem(0);
        viewpager.setOffscreenPageLimit(mTitles.length);
    }

    private void mSetTitle(int position) {
        switch (position) {
            case 0:
                EventBus.getDefault().post("");
                break;
            case 1:
                EventBus.getDefault().post("30,31");
                break;
            case 2:
                EventBus.getDefault().post("20");
                break;
            case 3:
                EventBus.getDefault().post("10");
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
        }
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {

        }
    }
}
